﻿using MCMesServer.DBFunctions.DBFuncs;
using MesDAL.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.DBFunctions.ReceiveFuncHandler
{
    public class StateChangeHandler
    {
        /// <summary>
        /// 改变设备状态
        /// </summary>
        /// <param name="beforeState"></param>
        /// <param name="afterState"></param>
        /// <param name="machineInfo"></param>
        /// <returns></returns>
        public static async Task<bool> ChangeMachineState(MachineInfoDTO machineInfo)
        {
            try
            {
                machineInfo = await MachineInfoDBFunc.EditMachineInfo(machineInfo);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 记录设备状态变更记录
        /// </summary>
        /// <param name="beforeState"></param>
        /// <param name="afterState"></param>
        /// <param name="machine_num"></param>
        /// <returns></returns>
        public static async Task<bool> RecordMachineStateLog(string beforeState, string afterState, string machine_num)
        {
            try
            {
                MachineStateLogDTO stateDto = new MachineStateLogDTO()
                {
                    before_state = beforeState,
                    after_state = afterState,
                    event_time = DateTime.Now,
                    machine_num = machine_num
                };
                stateDto = await MachineStateLogDBFunc.AddNewMachineStateLog(stateDto);
                if (stateDto.id == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
